package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.httputil.EntryPageName;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.UIUtils;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.DefaultHandler;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.mine.RegisterAccount;
import com.mmlc.network.filter.LoginOutRequestFilter;
import com.mmlc.utils.Constants;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.WebUtils;

/**
 * Created by Administrator on 2018/1/14.
 */

public class WebFragment extends InterFragment {
    @Override
    public int initResourceID() {
        getActivity().getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        return R.layout.activity_web_view;
    }

    String htmlUrl = "";
    TextView tv_title;

    @Override

    public void initExtra() {
        super.initExtra();

        htmlUrl = EntryPageName.globalRequestBean.getData().getTb_cate_h5();
    }


    BridgeWebView webView;
    ProgressBar progressBar;

    @Override
    public void initView() {
        webView = ViewUtils.getView(getActivity(), R.id.webview);
        progressBar = ViewUtils.getView(getActivity(), R.id.progress);
        tv_title=ViewUtils.getView(getActivity(),R.id.tv_title);

        webView.setDefaultHandler(new DefaultHandler());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDisplayZoomControls(true);
//        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setUserAgentString(webView.getSettings().getUserAgentString() + "mmlc.meiqi");
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setAppCachePath(Constants.DIR_CACHE);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);

//        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (htmlUrl.startsWith("http:") || htmlUrl.startsWith("https:")) {
//                    return false;
//                }
//                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                //view.loadUrl(htmlUrl);
//                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(htmlUrl));
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                getActivity().startActivity(intent);
//                return true;
//            }
//
//        });
        webView.requestFocus();
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100)
                    progressBar.setVisibility(View.GONE);
                else
                    progressBar.setVisibility(View.VISIBLE);
            }
        });
        WebUtils.setWebJS(webView,getActivity(),null);
//        if (ViewUtils.isEmpty(htmlUrl) && !ViewUtils.isEmpty(id))
//            htmlUrl = "http://wx.lolever.com/html/info_details.html?id=" + id;

        if (!ViewUtils.isEmpty(htmlUrl)) {
//        "http://security.tencent.com/lucky/check_tools.html"
            webView.loadUrl(htmlUrl);
        } else {
            ToastUtils.showToast("网络请求有误");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    public void initEvent() {
        super.initEvent();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);*/
    }

    private void requestLoginOUtFromServer() {
        final LoginOutRequestFilter filter = new LoginOutRequestFilter((BaseParentActivity) getActivity());
        /*filter.offerErrorParams(rootView);*/
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<String>() {
            @Override
            public void onSuccess(String result) {
                ToastUtils.showToast("退出登陆成功");
                App.user.setLoginState(false);
                App.user.removePassword();
                filter.saveTokenId(null);
                UIUtils.simpleStartActivity(RegisterAccount.class);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast("退出登陆失败");
            }
        });
    }
}
